/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.stepapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.me.androidresttest.WSCaller;
import org.me.stepapp.persistence.InsertLocation;
import org.me.stepapp.persistence.ListLocation;

/**
 *
 * @author Amber
 */
public class MainActivity extends Activity {
     // GUI component declarations
    TextView tv_user;
    TextView tv_password;
    EditText et_user;
    EditText et_password;
    Button login;
    Menu appMenu;

    public static final String MY_IPADDRESS = "128.237.242.174";
    private static final String NAMESPACE = "http://service.stepapp.me.org/";
    private static final String URL = "http://" + MY_IPADDRESS + ":8080/StepAppServer/StepAppService";
//    private static String SOAP_ACTION; // = "http://calc.me.org/add";
    private static String METHOD_NAME; // = "add";
    WSCaller caller = null;
    private String service_url;

    public String getService_url() {
        return service_url;
    }

    public void setService_url(String service_url) {
        this.service_url = service_url;
    }

    private void initCaller() {
        if (this.caller == null) {
            this.caller = new WSCaller(NAMESPACE, URL);
        }
    }

    private void logIn() {
        initCaller();
        TelephonyManager tMgr = (TelephonyManager) this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String mPhoneNumber = tMgr.getLine1Number();

        boolean hope = caller.newUser(mPhoneNumber, "myConputerEmulator");

        if (hope) {
            Toast.makeText(this, "Welcome to StepApp", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.welcome);
        }
        else {
            Toast.makeText(this, "New Users must Register Name and Phone Number", Toast.LENGTH_SHORT).show();
        }
    }

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        //this.setURLAddress("http://" + MY_IPADDRESS + ":8080/StepAppServer/resources/stepappUsers/");

        //  Add GUI components
        tv_user = (TextView)findViewById(R.id.tv_user);
        tv_password = (TextView)findViewById(R.id.tv_user);
        et_user = (EditText)findViewById(R.id.et_user);
        et_password = (EditText)findViewById(R.id.et_password);
        login = (Button)findViewById(R.id.btn_login);

        // Login Button Listener
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                // Future Function:  Validate Login here
                logIn();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)  {
        super.onCreateOptionsMenu(menu);
        addMenuItems(menu);
        return true;
    }

    /* Defines menu elements */
    private void addMenuItems(Menu menu)  {
        int base = Menu.FIRST;
        menu.add(base, base, base, "Tasks");
        menu.add(base, base+1, base+1, "My Locations");
        menu.add(base, base+2, base+2, "Add Location");
        menu.add(base, base+3, base+3, "Display Map");
        menu.add(base, base+4, base+4, " Quit! ");
    }

    /* Defines the actions when a menu item is selected */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)  {
        switch (item.getItemId()) {
            case 1:
                //setContentView(R.layout.tasks);
                Intent i1 = new Intent(this, Tasks.class);
                this.startActivity(i1);
                return true;
            case 2:
                Intent i2 = new Intent(this, ListLocation.class);
                this.startActivity(i2);
                return true;
            case 3:
                Intent i3 = new Intent(this, InsertLocation.class);
                this.startActivity(i3);
                return true;
            case 4:
                Intent i4 = new Intent(this, ShowMap.class);
                this.startActivity(i4);
                return true;
            case 5:
                finish();
        }
        return false;
    }
}

